package com.nobug.service.user;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nobug.bean.BaseListData;
import com.nobug.bean.user.*;
import com.nobug.mapper.userMappers.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    AddressMapper addressMapper;

    @Autowired
    CollectMapper collectMapper;

    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    HistoryMapper historyMapper;

    @Autowired
    FeedbackMapper feedbackMapper;

    @Override
    public BaseListData queryUsers(Integer page, Integer limit, String username, String mobile, String sort, String order) {
        PageHelper.startPage(page,limit);
        UserExample userExample = new UserExample();
        userExample.setOrderByClause(sort + " " + order);
        UserExample.Criteria criteria = userExample.createCriteria();
        if(username != null && username != ""){criteria.andUsernameLike("%" + username + "%");}
        if(mobile != null && mobile != ""){criteria.andMobileEqualTo(mobile);}
        List<User> users = userMapper.selectByExample(userExample);
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        long total = userPageInfo.getTotal();
        return BaseListData.ok(users,total);
    }

    @Override
    public BaseListData queryAddresses(Integer page, Integer limit, Integer userId, String name, String sort, String order) {
        PageHelper.startPage(page,limit);
        AddressExample addressExample = new AddressExample();
        addressExample.setOrderByClause(sort + " " + order);
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if(userId != null){criteria.andUserIdEqualTo(userId);}
        if(name != null && name != ""){criteria.andNameLike("%" + name + "%");}
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        PageInfo<Address> addressPageInfo = new PageInfo<>(addresses);
        long total = addressPageInfo.getTotal();
        return BaseListData.ok(addresses,total);
    }

    @Override
    public BaseListData queryCollect(Integer page, Integer limit, Integer userId, Integer valueId, String sort, String order) {
        PageHelper.startPage(page,limit);
        CollectExample collectExample = new CollectExample();
        collectExample.setOrderByClause(sort + " " + order);
        CollectExample.Criteria criteria = collectExample.createCriteria();
        if(userId != null){criteria.andUserIdEqualTo(userId);}
        if(valueId != null){criteria.andValueIdEqualTo(valueId);}
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        PageInfo<Collect> addressPageInfo = new PageInfo<>(collects);
        long total = addressPageInfo.getTotal();
        return BaseListData.ok(collects,total);

    }

    @Override
    public BaseListData queryFootprint(Integer page, Integer limit, Integer userId, Integer goodsId, String sort, String order) {
        PageHelper.startPage(page,limit);
        FootprintExample footprintExample = new FootprintExample();
        footprintExample.setOrderByClause(sort + " " + order);
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        if(userId != null){criteria.andUserIdEqualTo(userId);}
        if(goodsId != null){criteria.andGoodsIdEqualTo(goodsId);}
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        PageInfo<Footprint> footprintsPageInfo = new PageInfo<>(footprints);
        long total = footprintsPageInfo.getTotal();
        return BaseListData.ok(footprints,total);
    }

    @Override
    public BaseListData queryHistory(Integer page, Integer limit, Integer userId, String keyword, String sort, String order) {
        PageHelper.startPage(page,limit);
        HistoryExample historyExample = new HistoryExample();
        historyExample.setOrderByClause(sort + " " + order);
        HistoryExample.Criteria criteria = historyExample.createCriteria();
        if(userId != null){criteria.andUserIdEqualTo(userId);}
        if(keyword != null && keyword != ""){criteria.andKeywordLike("%" + keyword + "%");}
        List<History> histories = historyMapper.selectByExample(historyExample);
        PageInfo<History> historyPageInfo = new PageInfo<>(histories);
        long total = historyPageInfo.getTotal();
        return BaseListData.ok(histories,total);
    }

    @Override
    public BaseListData queryFeedback(Integer page, Integer limit, String username, Integer id, String sort, String order) {
        PageHelper.startPage(page,limit);
        FeedbackExample feedbackExample = new FeedbackExample();
        feedbackExample.setOrderByClause(sort + " " + order);
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        if(id != null){criteria.andIdEqualTo(id);}
        if(username != null && username != ""){criteria.andUsernameLike("%" + username + "%");}
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        PageInfo<Feedback> feedbackPageInfo = new PageInfo<>(feedbacks);
        long total = feedbackPageInfo.getTotal();
        return BaseListData.ok(feedbacks,total);
    }
}
